﻿using System;
using System.Collections.Generic;
using System.Data;
using Ue.Infrastructure;
using Ue.Infrastructure.RepositoryFramework.Repository;
using Ue.Qcdz.Domain.Collectors;

namespace Ue.Qcdz.Repositories.Collectors
{
    public class CollectorRepository : SqlServerRepository<Collector>, ICollectorRepository
    {
        public CollectorRepository()
            : this(null) { }

        public CollectorRepository(IUnitOfWork uniteOfWork)
            : base(uniteOfWork) { }



        protected override System.Data.IDataParameter[] BuildParameters(Collector entity)
        {
            //
            DateTime dt = new DateTime(2000, 1, 1);
            entity.LastDate = entity.LastDate < dt ? dt : entity.LastDate;
            //
            entity.Remark += "|||" + entity.Encode;
            entity.Remark += "|||" + entity.NewsOutskirts;
            //
            return new IDataParameter[]{
              this.RepositoryDriver.BuildParameter(CollectorFactory.FieldNames.EntityKey,entity.Key),
              this.RepositoryDriver.BuildParameter(CollectorFactory.FieldNames.SiteName,entity.SiteName??""),
              this.RepositoryDriver.BuildParameter(CollectorFactory.FieldNames.SiteUrl,entity.SiteUrl??""),           
              this.RepositoryDriver.BuildParameter(CollectorFactory.FieldNames.Outskirts,entity.Outskirts??""),
              this.RepositoryDriver.BuildParameter(CollectorFactory.FieldNames.ALink,entity.ALink??""),
              this.RepositoryDriver.BuildParameter(CollectorFactory.FieldNames.Title,entity.Title??""),     
              this.RepositoryDriver.BuildParameter(CollectorFactory.FieldNames.Editor,entity.Editor??""), 
              this.RepositoryDriver.BuildParameter(CollectorFactory.FieldNames.Content,entity.Content??""),  
              this.RepositoryDriver.BuildParameter(CollectorFactory.FieldNames.Reference,entity.Reference??""),  
              this.RepositoryDriver.BuildParameter(CollectorFactory.FieldNames.ImageUrl,entity.ImageUrl??""),  
              this.RepositoryDriver.BuildParameter(CollectorFactory.FieldNames.LastDate,entity.LastDate),                
              this.RepositoryDriver.BuildParameter(CollectorFactory.FieldNames.Status,entity.Status),
              this.RepositoryDriver.BuildParameter(CollectorFactory.FieldNames.Remark,entity.Remark??""),
              this.RepositoryDriver.BuildParameter(CollectorFactory.FieldNames.CreateOn,entity.CreateOn),
            };
        }

        protected override IWhereClause BuildWhereClauseBy(Infrastructure.DomainBase.IDataQuery dataQuery)
        {
            if (dataQuery is ICollectorDataQuery)
            {
                return new CollectorDataQueryWhereClause(dataQuery as ICollectorDataQuery);
            }

            return null;
        }

        protected override string[] GetBaseInsertFields()
        {
            return new string[] {
                   CollectorFactory.FieldNames.EntityKey,
                   CollectorFactory.FieldNames.SiteName,
                   CollectorFactory.FieldNames.SiteUrl,
                   CollectorFactory.FieldNames.Outskirts,
                   CollectorFactory.FieldNames.ALink,
                   CollectorFactory.FieldNames.Title,
                   CollectorFactory.FieldNames.Editor,
                   CollectorFactory.FieldNames.Content,   
                   CollectorFactory.FieldNames.Reference,
                   CollectorFactory.FieldNames.ImageUrl,
                   CollectorFactory.FieldNames.LastDate,
                   CollectorFactory.FieldNames.Status,
                   CollectorFactory.FieldNames.Remark,
                   CollectorFactory.FieldNames.CreateOn
            };
        }

        protected override string[] GetBaseUpdateFields()
        {
            return new string[] {
                   CollectorFactory.FieldNames.SiteName,
                   CollectorFactory.FieldNames.SiteUrl,
                   CollectorFactory.FieldNames.Outskirts,
                   CollectorFactory.FieldNames.ALink,
                   CollectorFactory.FieldNames.Title,
                   CollectorFactory.FieldNames.Editor,
                   CollectorFactory.FieldNames.Content,   
                   CollectorFactory.FieldNames.Reference,
                   CollectorFactory.FieldNames.ImageUrl,
                   CollectorFactory.FieldNames.LastDate,
                   CollectorFactory.FieldNames.Status,
                   CollectorFactory.FieldNames.Remark
            };
        }

        protected override IDictionary<string, SqlServerRepositoryBase<Collector>.ChildDataHandler> GetBuildEntityChildCallbacks()
        {
            return null;
        }

        protected override string GetDataTableName()
        {
            return "CollectSetting";
        }
    }
}
